﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Config;
using AMM.Model;
using AMM.Model.Config;

namespace AMM.Business.Config {
    public class FunctionProcessor : BaseProcessor {

        private PowerHelper _powerHelper;
        private RoleRepository _roleRepository;
        private FunctionRepository _functionRepository;

        public FunctionProcessor() {
            this._powerHelper = new PowerHelper();
            this._roleRepository = new RoleRepository();
            this._functionRepository = new FunctionRepository();
        }

        public List<Function> FetchFunctionList(int roleId = 0) {
            try {
                if (roleId == 0) {
                    return this._functionRepository.FetchFunctionList();
                }
                else {
                    return this._functionRepository.FetchFunctionByRole(roleId);
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取功能列表失败！");
            }
        }

        public List<Function> FetchFunctionByParentId(int parentId) {
            try {
                return this._functionRepository.FetchFunctionByParentId(parentId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取功能列表失败！");
            }
        }

        public FunctionViewModel FetchFunctionByCode(string code) {
            try {
                Function func = this._functionRepository.FetchFunctionByCode(code);
                return new FunctionViewModel {
                    Key = func.FunctionId,
                    Code = func.Code,
                    Name = func.Name,
                    Description = func.Description,
                    DesignPowers = this._powerHelper.GetPowerDescriptions(func.DesignPowers)
                };
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取系统功能失败！");
            }
        }

        public RoleFunction FetchPowersByUserAndCode(int userId, string code) {
            try {
                List<Role> roleList = this._roleRepository.FetchRoleByUser(userId);
                List<RoleFunction> roleFuncList = this._functionRepository.FetchRoleFunctionByCode(code);

                RoleFunction result = new RoleFunction();
                foreach (RoleFunction rf in roleFuncList.OrderByDescending(x => x.Powers)) {
                    if (roleList.Any(y => y.RoleId == rf.RoleId)) {
                        result = rf;
                        break;
                    }
                };
                return result;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("未找到对应的权限编码！");
            }
        }

        public List<FunctionViewModel> FetchFunctionViewModelList(int roleId = 0) {
            try {
                List<Function> resultList;
                if (roleId == 0) {
                    resultList = this._functionRepository.FetchFunctionList();
                }
                else {
                    resultList = this._functionRepository.FetchFunctionByRole(roleId);
                }
                resultList.ForEach(x => {
                    x.DesignPowers = this._functionRepository
                        .FetchRoleFunctionByCode(x.Code).Find(y => y.FunctionId == x.FunctionId).Powers;
                });

                return resultList.Select(x => {
                    return new FunctionViewModel {
                        Key = x.FunctionId,
                        Code = x.Code,
                        Name = x.Name,
                        Description = x.Description,
                        CurrentPowers = x.DesignPowers,
                        DesignPowers = this._powerHelper.GetPowerDescriptions(x.DesignPowers)
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取功能列表失败！");
            }
        }
    }
}
